package com.dsp.week12.service;

import com.dsp.week12.model.MessageExt;
import io.github.majusko.pulsar.producer.PulsarTemplate;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MyProducer {

    @Value("${my-topic.test}")
    public String testTopic;
    @Value("${my-topic.msg}")
    public String msgTopic;
    @Value("${my-topic.obj}")
    public String objTopic;

    @Resource
    private PulsarTemplate<String> producer;
    @Resource
    private PulsarTemplate<String> msgProducer;
    @Resource
    private PulsarTemplate<MessageExt> messageExtPulsarTemplate;


    public void sendTest(String msg) {
        try {
            producer.send(testTopic, msg);
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(String msg) {
        try {
            msgProducer.send(msgTopic, msg);
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }
    }

    public void sendObj(MessageExt msg) {
        try {
            messageExtPulsarTemplate.send(objTopic, msg);
        } catch (PulsarClientException e) {
            e.printStackTrace();
        }
    }
}
